package com.smartleanx.module.hrm.task.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import com.smartleanx.framework.tenant.core.aop.TenantIgnore;
import com.smartleanx.framework.tenant.core.context.TenantContextHolder;
import com.smartleanx.framework.tenant.core.job.TenantJob;
import com.smartleanx.module.hrm.intergration.BpmServiceProvider;
import com.smartleanx.module.hrm.service.esign.eums.ActorTypeEnum;
import com.smartleanx.module.hrm.service.esign.service.HrmEContractTaskActorsService;
import com.smartleanx.module.hrm.service.esign.service.HrmEContractTaskService;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEContractTask;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEContractTaskActors;
import com.smartleanx.module.hrm.service.instation.InStationSmsService;
import com.smartleanx.module.hrm.service.instation.InstationSmsTypeEnum;
import com.smartleanx.module.hrm.service.sms.SmsService;
import com.smartleanx.module.hrm.service.sms.SmsTempEnum;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**

 *
 * @author xzq
 * @since 2025/8/18
 */
@Slf4j
@Component
public class ContractScheduled {

    @Resource
    private SmsService smsService;
    @Resource
    private InStationSmsService inStationSmsService;
    @Resource
    private HrmEContractTaskService hrmEContractTaskService;
    @Resource
    private HrmEContractTaskActorsService hrmEContractTaskActorsService;

    @Scheduled(cron = "0 0 1 * * ?")
    @TenantIgnore
    @DataPermission(enable = false)
    public void sendMessage() {
        DateTime begin = DateUtil.beginOfDay(new Date());
        DateTime end = DateUtil.endOfDay(new Date());
        List<HrmEContractTask> list = hrmEContractTaskService.list(new LambdaQueryWrapper<HrmEContractTask>()
                .eq(HrmEContractTask::getDataStatus, 1)
                .between(HrmEContractTask::getEffectiveDuration, begin, end));

        if(CollUtil.isEmpty(list)){
            return;
        }


        log.info("发送消息任务开始====================");
        for (HrmEContractTask contractTask : list) {
            //发送消息通知
            inStationSmsService.sendInstationSms(contractTask.getId(), InstationSmsTypeEnum.SIGN.getCode());
            //发送短信通知
            HrmEContractTaskActors personActor = hrmEContractTaskActorsService.getTaskActorListByTaskIdAndActorType(contractTask.getId(), ActorTypeEnum.PERSON.getCode());
            HrmEContractTaskActors corpActor = hrmEContractTaskActorsService.getTaskActorListByTaskIdAndActorType(contractTask.getId(), ActorTypeEnum.CORP.getCode());
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("name",corpActor.getActorName());
            jsonObject.set("contractName",contractTask.getContractName());
            smsService.sendSignSms(personActor.getPsnAccount(), SmsTempEnum.EXPIRED_SIGN.getCode(), jsonObject);
        }
        log.info("发送消息任务结束====================");

    }

}
